<?php

namespace app\api\controller\v1\user\base;

use app\admin\model\DataUser;
use app\admin\model\DataUserLotteryLog;
use app\admin\model\DataUserReal;
use app\api\controller\Api;


class Info extends Api
{

    /**
     * 获取用户信息
     * @return void
     */
    public function get(){
        $list = $this->getModel()->findOrEmpty();
        if($list->isEmpty())$this->error('用户不存在');
        $this->success('获取成功',$list);
    }


    /**
     * 修改用户信息
     * @return void
     */
    public function set(){
        $rule = [
            'heading'    => 'require',
            'nickname'    => 'require',
            'sex'    => 'require',
        ];
        $message  =   [
            'heading.require' => '头像不为空',
            'nickname.require' => '昵称不为空',
            'sex.require' => '性别不为空',
        ];
        $this->validate($this->post, $rule,$message);
        $list = $this->getModel()->findOrEmpty();
        if($list->isEmpty())$this->error('用户不存在');
        if($list->save($this->post)){
            $this->success('修改成功',$list);
        }else{
            $this->error('修改失败',$list);
        }

    }



    /**
     * 实名认证
     * @return void
     */
    public function real(){
        $rule = [
            'lottery_id' =>'require',
            'name'    => 'require',
            'phone'    => 'require|mobile',
            'bank'    => 'require',
            'bank_address'    => 'require',
            'bank_no'    => 'require',
        ];
        $message  =   [
            'lottery_id.require' => '中奖不为空',
            'name.require' => '真实姓名不为空',
            'phone.require' => '手机号不为空',
            'phone.mobile' => '手机号错误',
            'bank.require' => '银行不为空',
            'bank_address.require' => '支行地址不为空',
            'bank_no.require' => '银行卡号不为空',
        ];
        $this->validate($this->post, $rule,$message);
        $list = $this->getModel()->findOrEmpty();
        if($list->isEmpty())$this->error('用户不存在');
        $log =DataUserLotteryLog::where(['id'=>$this->post['lottery_id'],'status'=>0,'win'=>1])->findOrEmpty();
        if($log->isEmpty())$this->error('中奖记录不存在');
        $this->post['unid'] = $list->id;
        $real = DataUserReal::create($this->post);
        if($list->save($this->post)){
            $this->success('修改成功',$real);
        }else{
            $this->error('修改失败',$real);
        }

    }


    private function getModel(){
        return DataUser::where(['usertype'=>0,'id'=>$this->user['id']]);
    }


}